/**
  * @file	WHGM5.c
  *	@author	yoyoyes
  * @date	2023-6-10
  */
  
#include "WHGM5.h"
#include "usart.h"
#include "systick.h"
#include "string.h"
#include "stdio.h"
#include "gd32f10x.h"

/**
  * @func	WHGM5_init
  * @param	none
  * @return	none
  */
void WHGM5_init(void)
{
	rcu_periph_clock_enable(RCU_GPIOB);	  
	
	gpio_init(GPIOB, GPIO_MODE_IPD, GPIO_OSPEED_50MHZ, GPIO_PIN_0);        
	gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
	
	WHGM5_reset();
}

/**
  * @func	WHGM5_init
  * @param	none
  * @return	none
  */
void WHGM5_reset(void)
{
	usart0_printf("\r\n");
	usart0_printf("[info] 4G module init...\r\n");
	gpio_bit_set(GPIOB, GPIO_PIN_2);
	systick_delay_ms(500);
	gpio_bit_reset(GPIOB, GPIO_PIN_2);
	
	WHGM5_start_info();
}

/**
  * @func	WHGM5_start_info
  * @param	none
  * @return	none
  */
void WHGM5_start_info(void)
{
	usart_rx_frame_timeout_read(&usart2_rx_control_block, usart2_rx_frame, 15000);
	if(strcmp((const char *)usart2_rx_frame, "WH-GM5") == 0)
	{
		usart0_printf("[info] 4G module init success!\r\n");
	}
	else
	{
		usart0_printf("[info] 4G module init failed!\r\n");
	}
}

/**
  * @func	WHGM5_config_mode_enter
  * @param	none
  * @return	none
  */
void WHGM5_config_mode_enter(void)
{
	usart2_printf("+++");

	usart_rx_frame_timeout_read(&usart2_rx_control_block, usart2_rx_frame, 30);	
	if(strcmp((const char *)usart2_rx_frame, "a") == 0)
		usart2_printf("a");
			
	usart_rx_frame_timeout_read(&usart2_rx_control_block, usart2_rx_frame, 30);
	if(strcmp((const char *)usart2_rx_frame, "+ok\r\n") == 0)
		usart0_printf("[info] 4G module enter config mode!\r\n");
}

/**
  * @func	WHGM5_config_mode_exit
  * @param	none
  * @return	none
  */
void WHGM5_config_mode_exit(void)
{
	usart2_printf("AT+ENTM\r\n");
	
	usart_rx_frame_timeout_read(&usart2_rx_control_block, usart2_rx_frame, 30);
	if(strcmp((const char *)usart2_rx_frame, "AT+ENTM\r\n\r\nOK\r\n") == 0)
	{
		usart0_printf("[info] 4G module exit config mode!\r\n");
	}
}

/**
  * @func	WHGM5_server_info_set
  * @param	none
  * @return	none
  */
void WHGM5_server_info_set(void)
{
	char server_info[50];
	char at_server_info[50];
	char protocol[5], ip[30], port[5];
	
	WHGM5_config_mode_enter();
	
	usart0_printf("[info] Input server connect info (for example: TCP,test.usr.cn,2317)\r\n");
	usart0_printf("[info] ");
	usart0_gets(server_info, sizeof(server_info) / sizeof(char));
	
	sprintf(at_server_info, "AT+SOCKA=%s\r\n\r\nOK\r\n", server_info);
	
	usart2_printf("AT+SOCKA=%s\r\n", server_info);
	systick_delay_ms(30);
	usart2_printf("AT+SOCKAEN=ON\r\n");
	systick_delay_ms(30);
	usart2_printf("AT+SOCKBEN=OFF\r\n");
	systick_delay_ms(30);
	usart2_printf("AT+SOCKCEN=OFF\r\n");
	systick_delay_ms(30);
	usart2_printf("AT+SOCKDEN=OFF\r\n");
	systick_delay_ms(30);
	usart2_printf("AT+HEART=ON,NET,USER,60,C000\r\n");
	systick_delay_ms(30);
	usart2_printf("AT+S\r\n");
	systick_delay_ms(30);

	while(1)
	{
		usart_rx_frame_timeout_read(&usart2_rx_control_block, usart2_rx_frame, 100);
		
		if(strcmp((const char *)usart2_rx_frame, "AT+S\r\n") == 0)
		{
			usart_rx_frame_timeout_read(&usart2_rx_control_block, usart2_rx_frame, 100);
			if(strcmp((const char *)usart2_rx_frame, "\r\nOK\r\n") == 0)
			{
				sscanf(server_info, "%[^,],%[^,],%s", protocol, ip, port);
				usart0_printf("[info] Server info set success!\r\n\r\n"); 
				usart0_printf("       protocol : %s\r\n", protocol);
				usart0_printf("       ip       : %s\r\n", ip);
				usart0_printf("       port     : %s\r\n\r\n", port);
				
				break;
			}
		}
	}
}

/**
  * @func	WHGM5_net_rssi_get
  * @param	none
  * @return	none
  */
void WHGM5_net_rssi_get(void)
{ 
	WHGM5_config_mode_enter();
	
	usart2_printf("AT+ICCID?\r\n");

	usart_rx_frame_timeout_read(&usart2_rx_control_block, usart2_rx_frame, 100);
	usart0_printf("%s\r\n", usart2_rx_frame);
	
	WHGM5_config_mode_exit();
}
